﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoolERP_Entities;
using CoolERP_DAL;

namespace CoolERP_BLL
{
    public class CGDBLL:BaseBLL<CGD>
    {
        public CGDBLL() { }
        public List<CGDModel> GetCGD(Dictionary<string, string> dc, bool isDistinct, string orderExpr, int? pageIndex, int pageSize, out int totalCount)
        {
            var predicate = PredicateExtensions.True<CGDModel>();
            if (dc != null && dc.Count > 0)
            {
                if (dc.ContainsKey("ID") && !string.IsNullOrEmpty(dc["ID"]))
                {
                    Guid temp = Guid.Parse(dc["ID"]);
                    predicate = predicate.AndEx(t => t.ID == temp);
                }
                if (dc.ContainsKey("BH") && !string.IsNullOrEmpty(dc["BH"]))
                {
                    string keyVal = dc["BH"];
                    predicate = predicate.AndEx(t => t.BH == keyVal);
                }
                if (dc.ContainsKey("ZT") && !string.IsNullOrEmpty(dc["ZT"]))
                {
                    int temp = Convert.ToInt32(dc["ZT"]);
                    predicate = predicate.AndEx(t => t.ZT == temp);
                }
                if (dc.ContainsKey("BMID") && !string.IsNullOrEmpty(dc["BMID"]))
                {
                    Guid temp = Guid.Parse(dc["BMID"]);
                    predicate = predicate.AndEx(t => t.BMID == temp);
                }
                if (dc.ContainsKey("GYSID") && !string.IsNullOrEmpty(dc["GYSID"]))
                {
                    Guid temp = Guid.Parse(dc["GYSID"]);
                    predicate = predicate.AndEx(t => t.GYSID == temp);
                }
                if (dc.Keys.Contains("CJSJ") && !string.IsNullOrEmpty(dc["CJSJ"]))
                {
                    DateTime keyVal = DateTime.ParseExact(Convert.ToDateTime(dc["CJSJ"]).ToString("yyyyMMdd"), "yyyyMMdd", System.Threading.Thread.CurrentThread.CurrentCulture);
                    predicate = predicate.AndEx(t => t.CJSJ.Date == keyVal.Date);
                }
                if (dc.Keys.Contains("RQ") && !string.IsNullOrEmpty(dc["RQ"]))
                {
                    DateTime keyVal = DateTime.ParseExact(Convert.ToDateTime(dc["RQ"]).ToString("yyyyMMdd"), "yyyyMMdd", System.Threading.Thread.CurrentThread.CurrentCulture);
                    predicate = predicate.AndEx(t => t.RQ == keyVal);
                }
                if (dc.Keys.Contains("DHSJ") && !string.IsNullOrEmpty(dc["DHSJ"]))
                {
                    DateTime keyVal = DateTime.ParseExact(Convert.ToDateTime(dc["DHSJ"]).ToString("yyyyMMdd"), "yyyyMMdd", System.Threading.Thread.CurrentThread.CurrentCulture);
                    predicate = predicate.AndEx(t => t.DHSJ == keyVal);
                }
                if (dc.ContainsKey("RYName") && !string.IsNullOrEmpty(dc["RYName"]))
                {
                    string temp = dc["RYName"];
                    predicate = predicate.AndEx(t => t.RYName == temp);
                }
                if (dc.ContainsKey("CZRYName") && !string.IsNullOrEmpty(dc["CZRYName"]))
                {
                    string temp = dc["CZRYName"];
                    predicate = predicate.AndEx(t => t.CZRYName == temp);
                }
            }
            ICGDDAL _dal = new CGDDAL(context);
            return _dal.GetCGD(predicate.Compile(), isDistinct, orderExpr, pageIndex, pageSize, out totalCount);
        }
        
        public bool CGDConfirm(Guid ID)
        {
            ICGDDAL _dal = new CGDDAL(context);
            return _dal.CGDConfirm(ID);
        }
    }
}
